package com.example.a22310.myapplication.Activity;


import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.databinding.DataBindingUtil;
import android.os.Bundle;
import android.os.Handler;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.LinearLayoutManager;

import com.example.a22310.myapplication.Adapter.MvvmAdapter;
import com.example.a22310.myapplication.Beans.Post;
import com.example.a22310.myapplication.Beans.PostComment;
import com.example.a22310.myapplication.Listener.PostListener;
import com.example.a22310.myapplication.Model.PostModel;
import com.example.a22310.myapplication.ViewModel.Attention.AddAttentionViewModel;
import com.example.a22310.myapplication.ViewModel.Post.AddCommentViewModel;
import com.example.a22310.myapplication.ViewModel.Post.AddLoveNumberViewModel;
import com.example.a22310.myapplication.ViewModel.Post.PostCommViewModel;
import com.example.a22310.myapplication.ViewModel.Post.PostDetailViewModel;
import com.example.a22310.myapplication.databinding.ActivityPostDetailBinding;

import java.util.List;

import static com.example.a22310.myapplication.ViewModel.Post.AddCommentViewModel.SENDFLAG;

public class PostDetail extends AppCompatActivity implements PostListener<PostComment> {
    private ActivityPostDetailBinding binding;
    private String FILENAME = "yuejian'data";
    private Post.PostlistBean post;
    int post_id,user_id,flag;
    private Handler handler = new Handler();
    private Runnable runnable = new Runnable() {
        public void run() {
            this.update();
            handler.postDelayed(this, 500);// 间隔1秒
        }
        void update() {
            if (SENDFLAG == true){
                PostModel postModel=new PostModel();
                postModel.postComm(PostDetail.this,user_id,post_id,1);
                SENDFLAG = false;
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this, R.layout.activity_post_detail);
        Intent intent=getIntent();
        post=(Post.PostlistBean)intent.getSerializableExtra("post");
        post_id = post.getPostid();
        new PostDetailViewModel(this, binding,post_id);
        new PostCommViewModel(this, binding,user_id,post_id,1);
        new AddCommentViewModel(this,binding,post_id,user_id,getUserid());
        System.out.println("----->>>>"+getUserid());
        new AddLoveNumberViewModel(this,binding,post_id,getUserid());
        String str =  post.getPhoto().substring(104,post.getPhoto().length());
        new AddAttentionViewModel(this,binding,user_id,post.getUserid(),post.getUsername(),str,0);
        binding.setPost(post);
        handler.postDelayed(runnable, 500);
    }



    private int  getUserid(){
        int newsNumber;
        SharedPreferences sharedPreferences = getSharedPreferences(FILENAME, Context.MODE_PRIVATE);
        newsNumber = sharedPreferences.getInt("userId",0);
        return newsNumber;
    }

    @Override
    protected void onResume() {
        super.onResume();

    }

    @Override
    public void onSuccess(PostComment postComment) {

        if(postComment.getCode()!=404){
            List<PostComment.ComListBean> postlistBeen =  postComment.getComList();
            binding.recyclerViewComment.setLayoutManager(new LinearLayoutManager(PostDetail.this));

            MvvmAdapter adapter = new MvvmAdapter(PostDetail.this, R.layout.cardview_post_comment,
                    postlistBeen, BR.postcomm);
            binding.recyclerViewComment.setAdapter(adapter);


        }

    }


    @Override
    public void onFailure() {

    }




}
